package com.xb.loan.usercenter.user.user.service.impl;

import com.xb.loan.log.exception.BizException;
import com.xb.loan.usercenter.service.user.IUserPhoneValService;
import com.xb.loan.usercenter.service.user.bean.UserPhoneValVO;
import com.xb.loan.usercenter.service.user.constant.UserConst;
import com.xb.loan.usercenter.service.user.exception.IUserServiceException;
import com.xb.loan.usercenter.user.user.dao.UserPhoneValDao;
import com.xb.loan.usercenter.user.user.dao.bean.UserPhoneVal;
import com.xb.loan.usercenter.user.util.PropertyConf;
import com.xb.loan.util.cache.redis.comm.FormatOperation;
import com.xb.loan.util.common.ObjectOperation;
import com.xb.loan.util.log.DefaultLoggerFacotry;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 类  描  述 : 用户信息处理业务类
 */
@Service("userPhoneValService")
public class UserPhoneValServiceImpl implements IUserPhoneValService,IUserServiceException {

    private static final Logger logger = DefaultLoggerFacotry.getLogger(UserPhoneValServiceImpl.class);

    @Autowired
    private UserPhoneValDao userPhoneValDao;

    @Autowired
    private PropertyConf propertyConf;
    
    @Override
    public int addPhoneValidate(UserPhoneValVO userPhoneValVO) throws BizException {
    	Map<String,Object> map = new HashMap<String,Object>();
    	map.put("phone", userPhoneValVO.getPhone());
    	map.put("type", userPhoneValVO.getType());
    	userPhoneValDao.updatePhoneKeyInValid(map);
    	
        UserPhoneVal userPhoneVal = (UserPhoneVal) ObjectOperation.convertByClasses(userPhoneValVO, UserPhoneVal.class);
        String phonevalId = FormatOperation.assemblySeq("common", 4, FormatOperation.formatDateToYMDHms(new Date()), "");
        userPhoneVal.setPhonevalId(phonevalId);
        userPhoneVal.setCreateTime(new Date());
        userPhoneVal.setInvalidTime(new Date(new Date().getTime()+propertyConf.getPhoneValInvalidTime()));
        userPhoneVal.setStatus(UserConst.UserPhoneValStatus.NOVALIDATE.getCode());
        return userPhoneValDao.insertSelective(userPhoneVal);
    }

    @Override
    public boolean validatePhone(String userId, String phone, String phoneKey) throws BizException {
        Map<String, String> map = new HashMap<String, String>();
        map.put("userId", userId);
        map.put("phone", phone);
        map.put("phoneKey", phoneKey);

        UserPhoneVal userPhoneVal = userPhoneValDao.validatePhone(map);
        if (userPhoneVal != null) {
            if (!compareTime(userPhoneVal.getInvalidTime())) {
                throw validateMissing;
            }

            userPhoneVal.setStatus(UserConst.UserPhoneValStatus.VALIDATE.getCode());
            userPhoneVal.setVerifyTime(new Date());
            if (userPhoneValDao.updateByPrimaryKey(userPhoneVal) > 0) {
                return true;
            } else {
                return false;
            }
        } else {
            throw userPhoneMissing;
        }
    }
    
    @Override
	public boolean validatePhone(String phone,
			String phoneKey) throws BizException {
    	Map<String, String> map = new HashMap<String, String>();
        map.put("phone", phone);
        map.put("phoneKey", phoneKey);
        UserPhoneVal userPhoneVal = userPhoneValDao.validatePhone(map);
        if (userPhoneVal != null) {
            if (!compareTime(userPhoneVal.getInvalidTime())) {
                throw validateMissing;
            }
            return true;
        } else {
            throw userPhoneMissing;
        }
	}
    
    /**
     * 验证手机、邮箱失效时间是否失效
     * @param invalidTime    失效时间
     * @return    返回boolean,true为不失效、false为失效
     */
    private boolean compareTime(Date invalidTime) {
        long sysdate = (new Date()).getTime();
        if (sysdate < invalidTime.getTime()) {
            return true;
        } else {
            return false;
        }
    }

    
}
